import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/AppViews/EngineerOrderView/model/meter_info_model.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import 'dart:convert' as convert;
import '../model/normal_config_model.dart';

Reducer<EngineerGasResultViewState> buildReducer() {
  return asReducer(
    <Object, Reducer<EngineerGasResultViewState>>{
      EngineerGasResultViewAction.action: _onAction,
      EngineerGasResultViewAction.onGetData: _onGetData,
      EngineerGasResultViewAction.onTapStatus: _onTapStatus,
    },
  );
}

EngineerGasResultViewState _onTapStatus(
    EngineerGasResultViewState state, Action action) {
  final EngineerGasResultViewState newState = state.clone()
    ..isSolve = action.payload;
  return newState;
}

EngineerGasResultViewState _onAction(
    EngineerGasResultViewState state, Action action) {
  final EngineerGasResultViewState newState = state.clone();
  return newState;
}

EngineerGasResultViewState _onGetData(
    EngineerGasResultViewState state, Action action) {
  final EngineerGasResultViewState newState = state.clone();
  var json = action.payload;
  String dispatchLog = CommonUtils.checkNullToString(json['bizDispatchlog']);
  if (dispatchLog.isNotEmpty) {
    newState.logList = jsonDecode(dispatchLog);
  }
  newState.isSolve = CommonUtils.checkNullToString(json['bizResult']) == '1';
  List<String> orderPhoto =
      CommonUtils.checkNullToString(json['bizMaintenancefiles']).isEmpty
          ? []
          : CommonUtils.checkNullToString(json['bizMaintenancefiles'])
              .split(',');
  newState.orderPhoto = orderPhoto;
  newState.orderSignPhoto = CommonUtils.checkNullToString(json['bizSignature']);
  if (CommonUtils.checkNullToString(action.payload['bizMaintenanceremark'])
      .isNotEmpty) {
    List list = convert.jsonDecode(action.payload['bizMaintenanceremark']);
    List<NormalConfigModel> items = list
        .map<NormalConfigModel>((e) => NormalConfigModel.fromResultJson(e))
        .toList();
    newState.items = items;
  } else {
    newState.items = [];
  }

  List materList = action.payload['businessMaterialRelList'] ?? [];
  newState.materList = materList
      .map<EngineerMaterList>((e) => EngineerMaterList.fromDetail(e))
      .toList();

  Map extentInfo = action.payload['securitycheckBusinessExtend'] ?? {};
  String gasMeterImg = CommonUtils.checkNullToString(extentInfo['gasMeterImg']);
  if (gasMeterImg.isNotEmpty) {
    newState.gasMeterImg = gasMeterImg.split(',');
  }
  String businessLicenseImg =
      CommonUtils.checkNullToString(extentInfo['businessLicenseImg']);
  if (businessLicenseImg.isNotEmpty) {
    newState.businessLicenseImg = businessLicenseImg.split(',');
  }
  String pipelineImg = CommonUtils.checkNullToString(extentInfo['pipelineImg']);
  if (pipelineImg.isNotEmpty) {
    newState.pipelineImg = pipelineImg.split(',');
  }
  String vedioUrl = CommonUtils.checkNullToString(extentInfo['vedioUrl']);
  if (vedioUrl.isNotEmpty) {
    newState.videoUrl = vedioUrl.split(',');
  }
  String panoramagramImg =
      CommonUtils.checkNullToString(extentInfo['panoramagramImg']);
  if (panoramagramImg.isNotEmpty) {
    newState.panoramagramImg = panoramagramImg.split(',');
  }

  newState.idCard = CommonUtils.checkNullToString(extentInfo['idCard']);
  newState.idCardFrontImg =
      CommonUtils.checkNullToString(extentInfo['idCardFrontImg']);
  newState.idCardBackImg =
      CommonUtils.checkNullToString(extentInfo['idCardBackImg']);
  return newState;
}
